using System;
using System.Collections.Generic;
using System.Text;
using VSSDKAssist.Assets.Actions.Base;
using VSSDKAssist.Assets.Helpers;
using Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates;
using EnvDTE;
using Microsoft.Practices.RecipeFramework;

namespace VSSDKAssist.Recipes.Common.Actions
{
	/// <summary>
	/// Action that adds a call on the Dispose method
	/// </summary>
	public class AddVSPackageDisposeFunctionAction : ActionBase
	{
		private CodeClass codeClass;

		/// <summary>
		/// Gets or sets the code class.
		/// </summary>
		/// <value>The code class.</value>
		[Input(Required = true)]
		public CodeClass CodeClass
		{
			get { return codeClass; }
			set { codeClass = value; }
		}

		private CodeFunction codeFunction;

		/// <summary>
		/// Gets or sets the code function.
		/// </summary>
		/// <value>The code function.</value>
		[Output]
		public CodeFunction CodeFunction
		{
			get { return codeFunction; }
			set { codeFunction = value; }
		}

		/// <summary>
		/// Called when [execute].
		/// </summary>
		protected override void OnExecute()
		{
			CodeFunction disposeFunction =
				FileCodeModelHelper.GetCodeFunction(this.CodeClass, "Dispose");

			if(disposeFunction == null)
			{
				using(TextTemplateAction textTemplateAction = new TextTemplateAction())
				{
					textTemplateAction.Site = this.Site;
					textTemplateAction.Template = @"TT\Common\PackageDispose.tt";
					textTemplateAction.Execute();

					FileCodeModelHelper.InsertTextToCodeElement(
						this.CodeClass as CodeElement, textTemplateAction.Content);

					this.CodeFunction = FileCodeModelHelper.GetCodeFunction(this.CodeClass, "Dispose");
				}
			}
			else
			{
				this.CodeFunction = disposeFunction;
			}
		}
	}
}